<?php
	require_once dirname(BASEPATH) . '/application/models/local/data/cafe/_init_model_cafe.php';
	
	class mod_cafe_contact extends _init_model_cafe{
		public $CAF_P_L_ID;
		public $CAF_P_L_TITLE;
		public $CAF_P_L_ADDRESS;
		public $CAF_P_L_POSTCODE;
		public $CAF_P_L_PHONE;
		public $CAF_P_L_FAX;
		public $CAF_P_L_EMAIL;
		public $CAF_P_L_MAP;
		public $PROVINCE_ID;
		public $CAF_ID;
		
		function mod_cafe_contact(){
			parent::__construct();
			
			$this->tableName = "cafe_contact";
		}
		
		function get_single_contact($id, $cafe_id=null){
			$this->db->where('CAF_P_L_ID', $id);
			if (!is_null($cafe_id))
				$this->db->where('CAF_ID', $cafe_id);
				
			$query = $this->db->get($this->tableName);
			
			if ($query->num_rows() > 0){
				$row = $query->row_array(0);

				$this->load->library('define/directory_path');
				$row['CAF_P_L_MAP'] = $this->directory_path->img_map_path . $row['CAF_P_L_MAP'];
				
				return $row;
			} else
				return null;
		}
		function get_contact_of_cafe($cafe_id, $fields=null){
			if (!is_null($fields))
				$this->db->select($fields);
				
			$this->db->where('CAF_ID', $cafe_id);
			$this->db->join('province', 'province.PROVINCE_ID = cafe_contact.PROVINCE_ID');
			$this->db->order_by('CAF_P_L_ID', 'DESC');
			$query = $this->db->get($this->tableName);			
			$return = $query->result_array();
			
			if (count($return) > 0){
				$this->load->library('define/directory_path');
								
				for ($i=0; $i<count($return); $i++){
					if (isset($return[$i]['CAF_P_L_MAP'])){
						if (empty($return[$i]['CAF_P_L_MAP']))
							$return[$i]['CAF_P_L_MAP'] = $this->directory_path->img_no_map_path;
						else
							$return[$i]['CAF_P_L_MAP'] = $this->directory_path->img_map_path . $return[$i]['CAF_P_L_MAP'];
					}				
				}				
			}
			
			return $return;		
		}
		function get_province_list_of_cafe($cafe_id){
			$this->db->select('province.PROVINCE_NAME');
			$this->db->where('CAF_ID', $cafe_id);
			$this->db->join('province', 'province.PROVINCE_ID = cafe_contact.PROVINCE_ID');			
			$query = $this->db->get($this->tableName);
			
			$return = array();
			if ($query->num_rows() > 0){
				foreach ($query->result() as $row)
					array_push($return, $row->PROVINCE_NAME);
			}
			
			return $return;
		}
		function get_map_filename($id){
			$this->db->select('CAF_P_L_MAP');
			$this->db->where('CAF_P_L_ID', $id);
			$query = $this->db->get($this->tableName);
			
			if ($query->num_rows() > 0)
				return $query->row(0)->CAF_P_L_MAP;
			else
				return null;
		}
	
		function add(){
			$data = array('CAF_P_L_TITLE' => $this->CAF_P_L_TITLE,
							'CAF_P_L_ADDRESS' => $this->CAF_P_L_ADDRESS,
							'CAF_P_L_POSTCODE' => $this->CAF_P_L_POSTCODE,
							'CAF_P_L_PHONE' => $this->CAF_P_L_PHONE,
							'CAF_P_L_FAX' => $this->CAF_P_L_FAX,
							'CAF_P_L_EMAIL' => $this->CAF_P_L_EMAIL,
							'CAF_P_L_MAP' => $this->CAF_P_L_MAP,
							'PROVINCE_ID' => $this->PROVINCE_ID,
							'CAF_ID' => $this->CAF_ID);
			return $this->db->insert($this->tableName, $data);
		}
		function edit($contact_id, $cafe_id){
			$data = array();
			if (!empty($this->CAF_P_L_TITLE))
				$data['CAF_P_L_TITLE'] = $this->CAF_P_L_TITLE;
			if (!empty($this->CAF_P_L_ADDRESS))
				$data['CAF_P_L_ADDRESS'] = $this->CAF_P_L_ADDRESS;
			if (!empty($this->CAF_P_L_POSTCODE))
				$data['CAF_P_L_POSTCODE'] = $this->CAF_P_L_POSTCODE;
			if (!empty($this->CAF_P_L_PHONE))
				$data['CAF_P_L_PHONE'] = $this->CAF_P_L_PHONE;
			if (!empty($this->CAF_P_L_FAX))
				$data['CAF_P_L_FAX'] = $this->CAF_P_L_FAX;
			if (!empty($this->CAF_P_L_EMAIL))
				$data['CAF_P_L_EMAIL'] = $this->CAF_P_L_EMAIL;
			if (!empty($this->CAF_P_L_MAP)){
				$data['CAF_P_L_MAP'] = $this->CAF_P_L_MAP;
				
				$map_filename = $this->get_map_filename($contact_id);
				if (!is_null($map_filename)){
					$this->load->library('define/directory_path');
					unlink($this->directory_path->img_upload_cafe_map . $map_filename);
				}
			}
			if (!empty($this->PROVINCE_ID))
				$data['PROVINCE_ID'] = $this->PROVINCE_ID;
			
			if (count($data) > 0){				
				$this->db->where('CAF_ID', $cafe_id);
				return $this->db->update($this->tableName, $data);
			} else
				return true;				
		}
		function remove($contact_id){
			$this->db->where('CAF_P_L_ID', $contact_id);
			if ($this->db->delete($this->tableName)) {
				$file = $this->get_map_filename($contact_id);
				
				if (!is_null($file)){
					// remove file from server
					$this->load->library('define/directory_path');					
					unlink($this->directory_path->img_upload_cafe_map . $this->CAF_P_L_MAP);
				}
				
				return true;
			} else
				return false;		
		}
		function remove_by_cafe($cafe_id){			
			$result = $this->get_contact_of_cafe($cafe_id, array('CAF_P_L_MAP'));

			$this->load->library('define/directory_path');
			for ($i=0; $i<count($result); $i++){
				if (!is_null($result[$i]['CAF_P_L_MAP']) || !empty($result[$i]['CAF_P_L_MAP']))
					unlink($this->directory_path->img_upload_cafe_map . $result[$i]['CAF_P_L_MAP']);
			}	

			return parent::remove_by_cafe($cafe_id);
		}
	}
?>